package customized_input;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class MyReducer extends Reducer<LongWritable, Text, Text, LongWritable> {
    private Text outKey = new Text();
    private LongWritable outValue = new LongWritable();

    @Override
    protected void reduce(LongWritable key, Iterable<Text> values, Reducer<LongWritable, Text, Text, LongWritable>.Context context)
            throws IOException, InterruptedException {
        System.out.println("odd number line or even number line: " + key);

        long sum = 0;
        for(Text value: values){
            sum += Long.parseLong(value.toString());
        }

        if(key.get() == 0){
            outKey.set("sum of odd numbers are: ");
        }else{
            outKey.set("sum of even numbers are: ");
        }
        outValue.set(sum);
        context.write(outKey, outValue);
    }
}
